<template>
  <el-autocomplete
    class="inline-input"
    v-model="currentValue"
    :fetch-suggestions="querySearchAsync"
    placeholder="请输入商品货号"
    @select="handleSelect" style="width: 100%"
    @blur="handleBlur"
  >
    <i
      class="el-icon-search el-input__icon"
      slot="suffix">
    </i>
    <template slot-scope="props">
      <div class="good-item-info">
        <div class="good-item-pic">
          <img :src="props.item|goodImg">
        </div>
        <div class="good-item-name">
          <div><span>商品码:</span> {{props.item.itemNo}}</div>
          <div><span>商品名称:</span> {{props.item.good|goodName}}</div>
        </div>
      </div>
    </template>
  </el-autocomplete>
</template>
<script>
  import AdminService from 'src/service/admin'
  import emitter from 'src/components/emitter'

  export default {
    name: 'HGoodChoose',
    mixins: [emitter],
    data() {
      return {
        currentValue: null,
        loading: false,
        oldquery: null,
        datas: null,
        search: {
          itemNo: null,
          pagination: {
            total: 0,
            page: 1,
            limit: 10
          },
          isShow: 1,
          status: 1,
          orderBy: 'id desc'
        },

      }
    },
    filters: {
      goodName: function (data) {
        if (data != null) {
          return data.name
        }
        return ''
      },
      goodImg: function (data) {
        if (data != null) {
          if (data.imgs != null && data.imgs.length > 0) {
            return data.imgs[0]
          }
        }
        return '#'
      }
    },
    watch: {
      value: function (newValue, oldValue) {
        if (newValue == null || newValue.trim() == '')
          return;
        const me = this
        if (newValue != this.currentValue) {
          AdminService.goodSkuByItemNO(this.value).then(data => {
            if (data != null && data.datas != null && data.datas.length > 0) {
              me.datas = data.datas
              me.oldquery = me.value
              me.currentValue = me.value
            }
          })
        }
      }
    },
    props: {
      value: {
        type: String
      },
      validateEvent: {
        type: Boolean,
        default: true
      },
      id: {
        type: Number
      }
    },
    methods: {
      handleSelect(item) {
        const me = this
        this.currentValue = item.itemNo
        me.oldquery = this.currentValue
        this.$emit('input', item.itemNo)
        this.$emit('change', item)
        if (this.validateEvent) {
          me.dispatch('ElFormItem', 'el.form.change', [this.currentValue])
        }
      },

      handleBlur: function (event) {
        this.$emit('input', this.currentValue)
        if (this.validateEvent) {
          this.dispatch('ElFormItem', 'el.form.change', [this.currentValue])
        }
      },
      querySearchAsync(queryString, cb) {
        const me = this
        if (queryString == me.oldquery && me.datas != null) {
          cb(me.datas)
        } else {
          if (queryString !== undefined && queryString !== '') {
            me.search.itemNo = queryString
          } else {
            me.search.itemNo = null
          }
          AdminService.searchGoodSku(me.search).then(data => {
            if (data != null) {
              me.oldquery = queryString
              me.datas = data.datas
              cb(me.datas)
            }
          })
        }
      }
    },
    mounted: function () {
      const me = this
      if (me.value != null) {
        AdminService.goodSkuByItemNO(this.value).then(data => {
          if (data != null && data.datas != null && data.datas.length > 0) {
            me.datas = data.datas
            me.oldquery = me.value
            me.currentValue = me.value
          }
        })
      }
    }
  }
</script>
